﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;
using System.Data.SqlClient;

namespace QLCN
{
    
    public partial class FrmLogin : Form
    {
        
        bool vitri = true;
        bool trenduoi = true;
        private SqlDataAdapter mydataAdapter;
        private SqlConnection myConnection;
        private SqlCommand myCommand;
        private DataSet myDataSet;
        private DataTable myTable;
        
        public FrmLogin()
        {
            InitializeComponent();
                        
        }
        public string MD5(string chuoi) //ham ma hoa pass
        {
            string pass_md5 = "";
            byte[] mang = System.Text.Encoding.UTF8.GetBytes(chuoi);

            MD5CryptoServiceProvider my_md5 = new MD5CryptoServiceProvider();
            mang = my_md5.ComputeHash(mang);

            foreach (byte b in mang)
            {
                pass_md5 += b.ToString("X2");

            }

            return pass_md5;
        }
        private void btnDangnhap_Click(object sender, EventArgs e)
        {
            Dangnhap();
        }
        private void Dangnhap()
        {
            string ChuoiKetNoi = "";
            string sql;
            string pass = txtpass.Text.ToString().Trim();
            string passMD5 = MD5(pass);
            ChuoiKetNoi = "Data Source = " + Chung.ServerName + ";" +
                            "Initial Catalog = " + Chung.DatabaseName + ";" +
                            "persist security info = true;" +
                            "User Id=" + Chung.UserDatabase +
                            "; Password='" + Chung.PasswordDatabase +
                            "'; Connect Timeout =100";
            
            //mo ket noi
            myConnection = new SqlConnection(ChuoiKetNoi);
            myConnection.Open();            

            if ((txtuser.Text == "") && (txtpass.Text == ""))
            {
                MessageBox.Show("Mời bạn nhập người sử dụng và mật khẩu vào !", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                txtuser.Focus();
            }
            else
            {
                sql = "Select * from Account left join DMQuyen on Account.Quyen= DMQuyen.IDQuyen" +
                " Where Ten=N'" + txtuser.Text.ToString().Trim() +"' and pass=N'" + passMD5.Trim() + "'";
                SqlDataAdapter data = new SqlDataAdapter(sql, myConnection);               
                DataSet ds = new DataSet();
                data.Fill(ds, "Account");
                //MessageBox.Show(ds.Tables["Admin"].Rows.Count.ToString());

                if (ds.Tables["Account"].Rows.Count == 0)
                {
                    MessageBox.Show("Bạn đăng nhập không thành công ! Vui lòng kiểm tra người dùng hoặc mật khẩu!", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    txtuser.Focus();
                }
                else
                {
                  
                    Chung.User = txtuser.Text;
                    Chung.PassWord = txtpass.Text;
                    DataRow dr = ds.Tables["Account"].Rows[0];
                    Chung.Quyen = dr["Quyen"].ToString().Trim();//"Quyen" là tên cột trong cơ sỡ dữ liệu! 
                    Chung.TenQuyen = dr["TenQuyen"].ToString().Trim();
                    //luu du lieu nguoi dung den CSDL vao table Login
                    Chung.Tinh= dr["Tinh"].ToString().Trim();
                    Chung.Quan = dr["QuanHuyen"].ToString().Trim();
                    Chung.Phuong= dr["Phuong"].ToString().Trim();
                    Chung.TenDonVi= dr["TenDonVi"].ToString().Trim();

                    this.Close();
                     //MessageBox.Show("Bạn đăng nhập thành công ! Chao mung ban den voi chuong trinh Quản lý cai nghiện!,'"+ a.ToString() +"' " , "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                     //MessageBox.Show(  Chung.Quyen  , "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                }
            }
           
        }
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (vitri==true)
            {
                this.Height = 510;
                vitri = false;
            }
            else
            {
                this.Height = 176;
                vitri = true;
            }
        }

        private void txtuser_MouseClick(object sender, MouseEventArgs e)
        {
            trenduoi = true;
        }

        private void txtpass_MouseClick(object sender, MouseEventArgs e)
        {
            trenduoi = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "a";
            else
                txtpass.Text += "a";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "b";
            else
                txtpass.Text += "b";

        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "c";
            else
                txtpass.Text += "c";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "d";
            else
                txtpass.Text += "d";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "e";
            else
                txtpass.Text += "e";
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "f";
            else
                txtpass.Text += "f";
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "j";
            else
                txtpass.Text += "j";
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "h";
            else
                txtpass.Text += "h";
        }

        private void button10_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "i";
            else
                txtpass.Text += "i";
        }

        private void button11_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "k";
            else
                txtpass.Text += "k";
        }

        private void button12_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "l";
            else
                txtpass.Text += "l";
        }

        private void button13_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "m";
            else
                txtpass.Text += "m";

        }

        private void button14_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "n";
            else
                txtpass.Text += "n";
        }

        private void button15_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "o";
            else
                txtpass.Text += "o";
        }

        private void button16_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "u";
            else
                txtpass.Text += "u";
        }

        private void button17_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "t";
            else
                txtpass.Text += "t";
        }

        private void button18_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "r";
            else
                txtpass.Text += "r";
        }

        private void button19_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "w";
            else
                txtpass.Text += "w";
        }

        private void button20_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "q";
            else
                txtpass.Text += "q";
        }

        private void button21_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "s";
            else
                txtpass.Text += "s";
        }

        private void button22_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "g";
            else
                txtpass.Text += "g";
        }

        private void button23_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "z";
            else
                txtpass.Text += "z";
        }

        private void button24_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "x";
            else
                txtpass.Text += "x";
        }

        private void button25_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "v";
            else
                txtpass.Text += "v";
        }

        private void button26_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "p";
            else
                txtpass.Text += "p";
        }

        private void button27_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "y";
            else
                txtpass.Text += "y";
        }

        private void button38_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += " ";
            else
                txtpass.Text += " ";
        }

        private void button39_Click(object sender, EventArgs e)
        {
           /* if (trenduoi == true)
                txtuser.Text =txtuser.Text -" ";
            else
                txtpass.Text = " ";*/
        }

        private void button28_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "0";
            else
                txtpass.Text += "0";
        }

        private void button37_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "9";
            else
                txtpass.Text += "9";
        }

        private void button36_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "8";
            else
                txtpass.Text += "8";
        }

        private void button35_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "7";
            else
                txtpass.Text += "7";
        }

        private void button34_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "6";
            else
                txtpass.Text += "6";
        }

        private void button29_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "1";
            else
                txtpass.Text += "1";
        }

        private void button30_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "2";
            else
                txtpass.Text += "2";
        }

        private void button31_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "3";
            else
                txtpass.Text += "3";
        }

        private void button32_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "5";
            else
                txtpass.Text += "5";
        }

        private void button33_Click(object sender, EventArgs e)
        {
            if (trenduoi == true)
                txtuser.Text += "4";
            else
                txtpass.Text += "4";
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            //xac dinh cac thong so ban dau
            string Path = Directory.GetCurrentDirectory();
            StreamReader f = new StreamReader(Path + @"\" + "Path.txt");
            Chung.ServerName = f.ReadLine();
            //MessageBox.Show(ServerName);
            Chung.DatabaseName = f.ReadLine();
            //MessageBox.Show(DatabaseName);
            Chung.UserDatabase = f.ReadLine();
            //MessageBox.Show(UserDatabase);
            Chung.PasswordDatabase = f.ReadLine();
            //MessageBox.Show(PasswordDatabase);
            Chung.DuongDanAnh = f.ReadLine();
            Chung.DuongDanrpt = f.ReadLine();
            f.Close();
        }

        

        private void txtpass_KeyDown(object sender, KeyEventArgs e)
        {
            
            switch (e.KeyValue)
            {
                case 13:
                    Dangnhap();
                    break;
                case 27:
                    this.Close();
                    break;
              
            }
            
        }

        private void txtuser_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyValue)
            {
                case 13:
                    Dangnhap();
                    break;
                case 27:
                    this.Close();
                    break;

            }
        }

       
      
    }
}
